﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.Apis;
using Google.Apis.Json;
using FanPages.Common.SearchEngine;
using FanPages.Model;
using FanPages.Client.Gui.Desktop.Contracts;
using FanPages.Client.Gui.Desktop.Controllers;

namespace FanPages.Client.Gui.Desktop.Views
{
    public partial class SearchForm : Form
    {
        ISearchController controller = new SearchController();

        public SearchForm()
        {
            InitializeComponent();
            ddlFanItems.DataSource = controller.GetAllFanItems();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            GoogleSearch search = new GoogleSearch();
            search.SearchCompleted += new EventHandler<SearchEventArgs>(search_SearchCompleted);
            search.Key = "AIzaSyAXNyZ66QycdkUq3X3b3GzPxDhxARkk7iE";
            search.CX = "005628374864706373109:vj19xcjs8dq";
            search.Num = 10;
            search.Search(txtSearch.Text);
        }

        void search_SearchCompleted(object sender, SearchEventArgs e)
        {
            this.gridResults.DataSource = e.Response.Items;
        }

        private void btnSaveResult_Click(object sender, EventArgs e)
        {
            var items = this.gridResults.DataSource as List<Item>;
            var fanItem = ddlFanItems.SelectedItem as FanItem;
            var searchResults = new List<SearchResult>();
            foreach (var item in items)
            {
                searchResults.Add(new SearchResult()
                {
                    CachedId = item.CacheId,
                    DisplayLink = item.DisplayLink,
                    FanItem = new FanItem() 
                    {
                        Code = fanItem.Code                        
                    },
                    HtmlSnippet = item.HtmlSnippet,
                    HtmlTile = item.HtmlTitle,
                    Link = item.Link,
                    ResultDate = DateTime.Now,
                    Snippet = item.Snippet
                });
            }

        }
    }
}
